package com.zy.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.model.sys.ZyUser;

import com.zy.resp.ZyResult;
import com.zy.util.JsonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ZySignController {


    /**
     * 登录方法
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/ajaxLogin", method = RequestMethod.POST)
    @ResponseBody
    public String ajaxLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody ZyUser userInfo) {
        Subject subject = SecurityUtils.getSubject();
        String password=new SimpleHash("MD5",userInfo.getPassword(),userInfo.getAccount(),2).toHex();
        UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getAccount(), password);
        token.setRememberMe(true);
        try {
            subject.login(token);
        } catch (IncorrectCredentialsException e) {
            return ZyResult.fail(1000,"密码错误");
        } catch (LockedAccountException e) {
            return ZyResult.fail(1000,"登录失败，该用户已被冻结");
        } catch (AuthenticationException e) {
            return ZyResult.fail(1000,"该用户不存在");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String,Object>   map=new HashMap<>();
        map.put("token",subject.getSession().getId());
        return ZyResult.success(map);
    }




    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public  String info(HttpServletRequest  request, HttpServletResponse response,
                        String token){
        System.out.println("token:"+token);
        Subject subject = SecurityUtils.getSubject();
        ZyUser activeUser = (ZyUser) subject.getPrincipal();
        if(activeUser!=null){
            Session session = subject.getSession();
            ZyUser user = (ZyUser) session.getAttribute("user");
            if(user==null){
                session.setAttribute("user", activeUser);
            }
        }
        subject.getSession().getId();
        Map<String,Object> map=new HashMap<>();
        map.put("token",subject.getSession().getId());
        map.put("introduction","我说刘艺");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name",activeUser.getName());
        String[] myList = new String[1];
        myList[0] = "admin";
        map.put("roles",myList);
        System.out.println(JsonUtil.toJSONString(map));
        return  ZyResult.success(map);
    }




    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     * @return
     */
    @RequestMapping(value = "/unauth")
    @ResponseBody
    public String unauth() {
        return ZyResult.fail(1000,"请先登录");
    }
}
